/**
 * @type {plugins.window.Menu}
 * @properties={typeid:35,uuid:"E0297632-AC05-4A2F-A8EC-096137406032",variableType:-4}
 */
var $menu;

/**
 * @properties={typeid:24,uuid:"1BD82E6F-11A4-4EF0-A031-F2883B3D154B"}
 */
function _botones_hide_btn_reset_fields(){
	
	elements.btn_editar.visible = true;
	elements.btn_eliminar.visible = true;
	elements.btn_opciones.enabled = true;

	elements.btn_aceptar.visible = false;
	elements.btn_cancelar.visible = false;
	elements.btn_aceptar_siguiente.visible = false;
	elements.btn_aceptar_eliminar.visible = false;
	elements.btn_cancelar_eliminar.visible = false;
}

/**
 * @properties={typeid:24,uuid:"8C9C5AA4-95B7-41E3-90D6-A7DCDFB2F5B2"}
 */
function _botones_doEliminar(){
	
	elements.btn_editar.visible = false;
	elements.btn_eliminar.visible = false;
	elements.btn_opciones.enabled = false;

	elements.btn_aceptar.visible = false;
	elements.btn_cancelar.visible = false;
	elements.btn_aceptar_siguiente.visible = false;
	elements.btn_aceptar_eliminar.visible = true;
	elements.btn_cancelar_eliminar.visible = true;
}

/**
 * @properties={typeid:24,uuid:"F084A193-1B19-41AA-8E8B-F4E0206F6560"}
 */
function _botones_doEdit(){
	
	elements.btn_opciones.enabled = false;
	elements.btn_editar.visible = false;
	elements.btn_eliminar.visible = false;
	
	elements.btn_aceptar.visible = true;
	elements.btn_cancelar.visible = true;
	if (forms[controller.getName()].foundset.getSelectedRecord().isEditing()){
		elements.btn_aceptar_siguiente.visible = true;
	}else{
		elements.btn_aceptar_siguiente.visible = false;
	}
	elements.btn_cancelar_eliminar.visible = false;
	elements.btn_aceptar_eliminar.visible = false;
	
	forms.pigeon_menu_navigator.controller.enabled = false;
}

/**
 * @properties={typeid:24,uuid:"829E4E29-48C9-4489-B010-BD54EF4FBA1B"}
 */
function _hide_btn_reset_fields(){
	
	if (forms[controller.getName()].hide_btn_reset_fields){
		forms[controller.getName()].hide_btn_reset_fields();
	}
	
	globals.hide_btn_reset_fields(controller.getName());
	_botones_hide_btn_reset_fields();
	
	forms.pigeon_menu_navigator.controller.enabled = true;
}

/**
 * @properties={typeid:24,uuid:"5CA55688-1550-4FC1-884D-C5C436123F02"}
 */
function _doEdit(){
	
	globals.doEdit(controller.getName());
	
	if (forms[controller.getName()].doEdit){
		forms[controller.getName()].doEdit();
	}
	
	_botones_doEdit();
}

/**
 * @properties={typeid:24,uuid:"5B2C3D18-7252-4F02-A79D-1EA4F9E277D3"}
 */
function _doDelete(){
	
	_hide_btn_reset_fields();
	_botones_doEliminar();
}

/**
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"B75C35EF-2CD3-42EF-9DF9-5EF7F03B9934"}
 */
function _aceptar(event) {
	
	var success = false;
	
	// Antes
	if (forms[controller.getName()].aceptar_antes){
		success = forms[controller.getName()].aceptar_antes(event);
		if (success == false){
			return;
		}
	}
	
	// Salvar datos
	globals.saveEdits_foundset(forms[controller.getName()].foundset);
	
	// Después
	if (forms[controller.getName()].aceptar_despues){
		forms[controller.getName()].aceptar_despues(event);
	}

	// Modo vista
	_hide_btn_reset_fields();
	
	// Pantalla Anterior
	if (foundset.getSelectedRecord().isNew()){
		if ($formulario_padre.foundset != null && $formulario_padre.frm != null){
			globals.ir_formulario(
				event,
				$formulario_padre.frm,
				$formulario_padre.foundset);
		}
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"71662EDD-B096-47FD-B1A1-9D256F3159D4"}
 */
function _aceptar_siguiente(event) {
	
	// Salvar datos
	globals.saveEdits_foundset(forms[controller.getName()].foundset);
	// Nuevo Registro
	foundset.newRecord();
}

/**
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"88676FB5-8003-4CFD-8941-CF3D282F4D34"}
 */
function _cancelar(event) {
	
	// Proceso anterior a la cancelación
	if (forms[controller.getName()].cancelar_antes){
		forms[controller.getName()].cancelar_antes(event);
	}
	
	// Cancelamos la edicion/alta
	globals.cancelEditing_foundset(forms[controller.getName()].foundset);
	
	// Proceso posterior a la cancelación
	if (forms[controller.getName()].cancelar_despues){
		forms[controller.getName()].cancelar_despues(event);
	}
	
	// Modo Lectura
	_hide_btn_reset_fields();
	
	// Activamos menú lateral
	forms.pigeon_menu_navigator.controller.enabled = true;
	
	// Pantalla Anterior
	if (foundset.getSelectedRecord().isNew()){
		if ($formulario_padre.foundset != null && $formulario_padre.frm != null){
			globals.ir_formulario(
				event,
				$formulario_padre.frm,
				$formulario_padre.foundset);
		}
	}
}

/**
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"D87DF1DA-E09F-42B4-9E88-CD1031A9CD35"}
 */
function _alta(event) {
	
	elements.btn_editar.visible = false;
	elements.btn_eliminar.visible = false;
	elements.btn_opciones.visible = false;
	
	elements.btn_aceptar.visible = true;
	elements.btn_cancelar.visible = true;
	elements.btn_aceptar_siguiente.visible = false;
	elements.btn_aceptar_eliminar.visible = false;
	elements.btn_cancelar_eliminar.visible = false;
	
	
	if (forms[controller.getName()].alta_antes){
		forms[controller.getName()].alta_antes(event);
	}
	
	if (forms[controller.getName()].alta){
		forms[controller.getName()].alta(event);
	}
	
	if (forms[controller.getName()].alta_despues){
		forms[controller.getName()].alta_despues(event);
	}
}

/**
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"4605602B-EE88-4DA7-B7E8-EF8186133927"}
 */
function _eliminar(event) {
	
	if (forms[controller.getName()].eliminar_antes){
		forms[controller.getName()].eliminar_antes(event);
	}
	
	elements.btn_editar.visible = false;
	elements.btn_eliminar.visible = false;
	elements.btn_opciones.visible = false;
	
	elements.btn_aceptar.visible = false;
	elements.btn_cancelar.visible = false;
	elements.btn_aceptar_siguiente.visible = false;
	elements.btn_aceptar_eliminar.visible = true;
	elements.btn_cancelar_eliminar.visible = true;
}

/**
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"224627B6-5858-4DBB-9D64-0702DA94B7A0"}
 */
function _opciones(event) {
	
	// Menú por defecto
	$menu = plugins.window.createPopupMenu();
	$menu.addMenuItem("ENTY", null);
	$menu.addSeparator();

	// Opciones propias de cada formulario
	if (forms[controller.getName()].opciones){
		forms[controller.getName()].opciones(event);
	}
	
	if (event.getSource()) {
		$menu.show(event.getSource());
	}
}

/**
 * Callback method when form is (re)loaded.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"05E187BC-D58C-4FDE-86A0-0E89EE4FA3F5"}
 * @AllowToRunInFind
 */
function _onLoad(event) {

	if (forms[controller.getName()].onLoad){
		forms[controller.getName()].onLoad(event);
	}
	
	/** @type {JSFoundSet<db:/pigeon_board/adm_t900_tablas>} */
	var fs_adm_t900_tablas = databaseManager.getFoundSet(scopes['adm_t900_tablas'].$NOMBRE_SERVIDOR,scopes['adm_t900_tablas'].$NOMBRE_TABLA);
	if (fs_adm_t900_tablas.find()){
		fs_adm_t900_tablas.s_nombre_tabla = databaseManager.getDataSourceTableName(controller.getDataSource());
		fs_adm_t900_tablas.search();
	}
	
	if (fs_adm_t900_tablas.getSize()){
		elements.lbl_header_txt.text = fs_adm_t900_tablas.s_descripcion_frm.toUpperCase();
		elements.lbl_icono.imageURL = "media:///iconos/" + fs_adm_t900_tablas.s_icono_claro;
	}else{
		elements.lbl_header_txt.text = databaseManager.getDataSourceTableName(controller.getDataSource());
		elements.lbl_icono.imageURL = null;
	}
	
	
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"6BCFBD05-7DE1-4914-AF2E-D6B67BD75D8D"}
 * @AllowToRunInFind
 */
function _onShow(firstShow, event) {
	
	if (forms[controller.getName()].onShow){
		forms[controller.getName()].onShow(firstShow, event);
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"8DC7D64F-05EB-4092-A430-31F26CBBF0C4"}
 */
function _ir_formulario_anterior(event) {
	
	if ($formulario_padre.frm != null && $formulario_padre.foundset != null){
		globals.ir_formulario(
			event,
			$formulario_padre.frm,
			$formulario_padre.foundset);
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"E37DF751-3835-4CA5-B778-3B16865BED6A"}
 */
function _aceptar_eliminar(event) {
	
	foundset.deleteRecord(foundset.getSelectedRecord());
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"3084D94F-46FF-4825-8EE4-6EBB3727026A"}
 */
function _cancelar_eliminar(event) {

	_hide_btn_reset_fields();
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"04600DCC-1812-45CA-8103-433C5EFC12E2"}
 */
function onShow(firstShow, event) {

	if ($formulario_padre.foundset != null && $formulario_padre.foundset != null){
		elements.btn_historico_anterior.enabled = true;
	}else{
		elements.btn_historico_anterior.enabled = false;
	}
}
